#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"

void TestHeap1()
{
	Heap hp;
	HeapInit(&hp);
	HeapPush(&hp, 5);
	HeapPush(&hp, 10);
	HeapPush(&hp, 60);
	HeapPush(&hp, 100);
	HeapPush(&hp, 99);
	HeapPush(&hp, 12);
	HeapPush(&hp, 16);
	while (!HeapEmpty(&hp))
	{
		printf("%d ", HeapTop(&hp));
		HeapPop(&hp);
	}
	printf("\n");
	HeapDestory(&hp);
}



void TestHeapSort()
{
	int a[] = { 1,2,5,4,100,22,165,12,167,123,111,0 };
	int sz = sizeof(a) / sizeof(a[0]);
	HeapSort(a, sz);
}

void CreatDate()
{
	int n = 100000;
	srand(time(0));
	const char* file = "date.txt";
	FILE* pf = fopen(file, "w");
	if (pf == NULL)
	{
		perror("fopen");
		return;
	}
	while (n--)
	{
		int x = rand() % 10000;
		fprintf(pf, "%d\n", x);
	}
	fclose(pf);

}
void PrintTopK(const char* file,int k)
{
	int* topk = (int*)malloc(sizeof(int) * k);
	if (topk == NULL)
	{
		perror("malloc\n");
		return;
	}
	FILE* pf = fopen(file, "r");
	if (pf == NULL)
	{
		perror("fopen\n");
		return;
	}
	for (int i = 0; i < k; i++)
	{
		fscanf(pf, "%d", &topk[i]);
	}
	for (int i = (k - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(topk, k, i);
	}
	int x = 0;
	int ret = fscanf(pf, "%d", &x);
	while (ret != EOF)
	{
		if (x > topk[0])
		{
			Swap(&x, &topk[0]);
			AdjustDown(topk, k, 0);
		}

		ret = fscanf(pf, "%d", &x);

	}
	for (int i = 0; i < k; i++)
	{
		printf("%d ", topk[i]);
	}
	free(topk);
	fclose(pf);
}
int main()
{
	//TestHeap1();

	//TestHeapSort();
	//CreatDate();
	PrintTopK("date.txt", 10);
	return 0;
}